package edu.iscas.appcheckclient.broadcastreceiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.util.Log;
import android.widget.Toast;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStreamReader;

import edu.iscas.appcheckclient.model.actions.MockedHttpResponse;
import edu.iscas.appcheckclient.model.actions.MockedIntent;
import edu.iscas.appcheckclient.model.actions.MockedLocation;
import edu.iscas.appcheckclient.service.RecordService;

/**
 * Created by dell on 2015/11/24.
 */
public class HookedContentReceiver extends BroadcastReceiver {

    public HookedContentReceiver() {
        super();
        recordService = RecordService.getRecordService();
    }

    public HookedContentReceiver(RecordService recordService) {
        super();
        HookedContentReceiver.recordService = recordService;
    }

    private static final String TAG = HookedContentReceiver.class.getSimpleName();
    private static RecordService recordService;
    public static final String HOOKED_INTENT_RECEIVER = "edu.iscas.appcheckclient.broadcastreceiver.SEND_INTENT";
    public static final String HOOKED_HTTP_MESSAGE_RECEIVER = "edu.iscas.appcheckclient.broadcastreceiver.SEND_HTTP_MESSAGE";
    public static final String HOOKED_LOCATION_RECEIVER = "edu.iscas.appcheckclient.broadcastreceiver.SEND_LOCATION";

    @Override
    public void onReceive(Context context, Intent intent) {
        if(recordService == null){
            Log.e(TAG, "RecordService is not accessible!");
        }
        switch(intent.getAction()) {
            case HOOKED_INTENT_RECEIVER:
                Log.d(TAG, "Received broadcast : SEND_INTENT");
                Intent hookedIntent = intent.getExtras().getParcelable("HOOKED_INTENT");
                int resultCode = intent.getExtras().getInt("RESULT_CODE");
                try {
                    MockedIntent mockedIntent = new MockedIntent(hookedIntent, resultCode);
                    recordService.handleMockedIntent(mockedIntent);
                } catch (RuntimeException e) {
                    e.printStackTrace();
                    Toast.makeText(context, e.getMessage(), Toast.LENGTH_SHORT).show();
                }
                break;
            case HOOKED_HTTP_MESSAGE_RECEIVER:
                Log.d(TAG, "Received broadcast : SEND_HTTP_MESSAGE");
                MockedHttpResponse mockedHttpResponse = new MockedHttpResponse(intent.getExtras());
                recordService.handleMockedHttpResponse(mockedHttpResponse);
                break;
            case HOOKED_LOCATION_RECEIVER:
                Log.d(TAG, "Received broadcast : SEND_LOCATION");
                Location hookedLocation = intent.getExtras().getParcelable("HOOKED_LOCATION");
                try{
                    MockedLocation mockedLocation = new MockedLocation(hookedLocation);
                    recordService.handleMockedLocation(mockedLocation);
                } catch (RuntimeException e){
                    e.printStackTrace();
                    Toast.makeText(context, e.getMessage(), Toast.LENGTH_SHORT).show();
                }
                break;
        }
    }
}
